package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.*;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page=setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void saveWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //向套餐表插入数据
        setmealMapper.insert(setmeal);

        //获取生成的套餐id
        Long setmealId = setmealMapper.selectSetmealId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });

        //保存套餐和菜品的关联关系
        for(SetmealDish setmealDish:setmealDishes){
            setmealDishMapper.insert(setmealDish);
        }

    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    public void deleteQuery(List<Long> ids) {
        for (Long id:ids){
            Setmeal setmeal=setmealMapper.selectByIds(id);
            if (setmeal.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }

        }
        for (Long id:ids){
            setmealMapper.deleteById(id);
            setmealMapper.deleteSDById(id);
        }

    }

    /**
     * 根据id回显套餐信息
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        //套餐基本信息
        Setmeal setmeal=setmealMapper.selectByIds(id);
        //查询套餐中的菜品
        List<SetmealDish> dishes=setmealDishMapper.getBySetmealId(id);

        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(dishes);

        return setmealVO;
    }

    @Override
    public void updateWith(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //修改套餐表基本信息
        setmealMapper.update(setmeal);
        //删除套餐内菜品
        setmealDishMapper.delete(setmeal.getId());
        //重新加入菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes!=null && setmealDishes.size()>0){

            setmealDishes.forEach(setmealDish ->
                    setmealDish.setSetmealId(setmeal.getId())
            );

            //向套餐商品表插入n条数据
            for(SetmealDish setmealDish:setmealDishes){
                setmealDishMapper.insert(setmealDish);
            }

        }
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        //判断能否启用，其中的菜品都没有禁售

        //1.查出所有的相关菜品id
        List<Long> dishIdList = setmealDishMapper.getDishId(id);
        //2.去dish表中查这些菜品有没有禁用的
        for (Long dishId:dishIdList){
            Dish dish = dishMapper.getById(dishId);
            if (dish.getStatus()==StatusConstant.DISABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }


        //调用update方法
        Setmeal setmeal=Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
